<?php

ignore_user_abort(true); // 后台运行，不受前端断开连接影响
set_time_limit(0);
ini_set('max_execution_time', '0');
chdir(dirname(__FILE__));
error_reporting(E_ERROR);

require_once ("../../log.php");
require_once ("../../config.php");	// 引入配置
require_once ("../api_tool.php");
require_once ("core.php");

$ic_list = AllQuery("SELECT * FROM item_category WHERE channel_id = 1 AND status = '1';");

foreach ($ic_list as $ic_value) {
	$resp = getTaobaoTbkDgOptimusMaterial($ic_value['category_id']);
 	foreach ($resp['result_list']['map_data'] as $map_data) {
		// 如果标题没有字设置为空字符串
		$detail_images = '';
		foreach ($map_data['small_images']['string'] as $key => $_images) {
			$detail_images .= $_images;

			end($map_data['small_images']['string']);
			if ($key !== key($map_data['small_images']['string'])) {
				$detail_images .= ',';
			}
		}

		var_dump($map_data['small_images']);

		$parent_category_ids = ',0,';
		if (!empty($ic_value['parent_category_ids'])) {
			$parent_category_ids .= "{$ic_value['parent_category_ids']},";
		}
		if ($map_data['coupon_amount'] == 0) {
			if (mt_rand(1, 100) > 10) {
				break;
			}
		}

		$good_list = array (
			'item_id'				=> $map_data['item_id'],							// 商品ID
			'name'					=> $map_data['short_title'],						// 商品名称
			'title'					=> $map_data['title'],								// 商品标题
			'image' 				=> $map_data['pict_url'],							// 商品图片
			'url'					=> $map_data['item_url'],							// 商品图片
			'price' 				=> $map_data['zk_final_price'],						// 商品价格
			'detail_images'			=> $detail_images,									// 展示图册
			'commission_rate' 		=> $map_data['commission_rate'],					// 佣金比率
			'coupon_url' 			=> "https:{$map_data['coupon_share_url']}",			// 领券链接
			'coupon_amount' 		=> $map_data['coupon_amount'],						// 优惠券面值
			'coupon_start_fee' 		=> $map_data['coupon_start_fee'],					// 优惠券起用金额
			'channel_id' 			=> 1,												// 渠道代码:1=淘宝, 2=京东, 3=拼多多
			'category_id' 			=> $map_data['level_one_category_id'],				// 商品类别ID
			'category_name' 		=> $map_data['level_one_category_name'],			// 商品类别
			'sub_category_id' 		=> $map_data['category_id'],						// 商品子类别ID
			'sub_category_name' 	=> $map_data['category_name'],						// 商品子类别
			'nick' 					=> $map_data['nick'],								// 店铺昵称
			'parent_category_ids'	=> $parent_category_ids,							// 主目录id
		);

		$t_key = "";
		$t_value = "";
		foreach ($good_list as $key => $value) {
			$t_key .= $key;
			$t_value .= "'{$value}'";
			end($good_list);
			if ($key !== key($good_list)) {
				$t_key .= ', ';
				$t_value .= ', ';
			}
		}
		simQuery("INSERT INTO item_ordinary ({$t_key}) VALUES ({$t_value})");
	}
}

?>